#include <stdio.h>
#include <ctype.h>

#define BUFSIZE 100

    char buf[BUFSIZE];    /* buffer for ungetch */
    int bufp = 0;         /* next free position in buf */

    int push_back_char = 'd';
    int char_is_pushed_back = 0;

// @deprecated   
   int getch(void)  /* get a (possibly pushed-back) character */
   {
	if(char_is_pushed_back)
	  {
	    if (char_is_pushed_back!=-1)
		 {
		   char_is_pushed_back = 0;
		   return push_back_char;
		 }
	    else
		 {
   		   char_is_pushed_back = 0;
		   return '\0';
		 }
	  }
       return (bufp > 0) ? buf[--bufp] : getchar();
   }

// @deprecated
   void ungetch(int c)   /* push character back on input */
   {
	/*       if (bufp >= BUFSIZE)
           printf("ungetch: too many characters\n");
       else
	  buf[bufp++] = c;*/
	  
	  push_back_char = c;
	  char_is_pushed_back = 1;
   }

// @deprecated
   void ungets(char s[])   /* push string back on input */
   {
	if (s == 0) return; //we need a valid string. null will not do.
	for (int i =0 ; ; i++)
	  {
	    if (s[i] == 0) break; // we've reached the end of the string
	    else
		 {
		   ungetch(s[i]);
		 }
	  } 
   }
